<template>
  <el-card style="width: 500px">
    <el-form :model="form" label-width="80px">
      <el-upload
          class="avatar-uploader"
          action="http://localhost:9090/file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
      >
        <img v-if="form.headImage" :src="form.headImage" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item label="用户id"  v-if="false">
        <el-input v-model="form.userId" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户名称" >
        <el-input v-model="form.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" >
        <el-input v-model="form.passward" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户手机" >
        <el-input v-model="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户地址" >
        <el-input v-model="form.address" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save" >确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import request from "@/util/request";

export default {
  name:"userPerson",
  data(){
    return{
      form:{},
      user: localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
    }
  },
  mounted() {

    this.getUser().then(res=>{
      this.form=res;
    })
  },
  methods:{
    async getUser(){
      return  (await request.get("/user/userId/" + this.user.userId)).data;
    },

   async save(){
     await  request.post("/saveUser",this.form).then(res=>{

        if(res){
          this.$message.success("保存成功！")
          //更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAvatarSuccess(res){
      this.form.headImage=res;
    }
  }
}

</script>

<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>